package cn.wl.pchart.service.impl;

import cn.wl.pchart.dao.DynamicMapper;
import cn.wl.pchart.dao.UserMapper;
import cn.wl.pchart.entity.Dinfo;
import cn.wl.pchart.entity.Dynamic;
import cn.wl.pchart.entity.Dynamics;
import cn.wl.pchart.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

// 动态服务实现
@Service("dynamicService")
public class DynamicServiceImpl implements DynamicService {

    @Autowired
    DynamicMapper dynamicMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public Dynamics getDynamics(String id, String time) {

        BigInteger did = dynamicMapper.getDynamic(id, time);
        // System.out.println("获取到的did：" + did);
        Dynamics dynamics = getDynamics(did);
        if (userMapper.isLike(id, dynamics.getDynamic().getId()) == 1)
        {
            dynamics.setLike(true);
        } else {
            dynamics.setLike(false);
        }
        return dynamics;
    }

    @Override
    public Dynamics getDynamics() {
        return getDynamics(dynamicMapper.getDynamicRand());
    }

    @Override
    public List<Dynamics> getDynamicsById(String id) {
        List<BigInteger> dids = dynamicMapper.getDynamicById(id);
        List<Dynamics> dynamicsList = new ArrayList<Dynamics>();
        for (BigInteger i : dids) {
            dynamicsList.add(getDynamics(i));
        }
        return dynamicsList;
    }

    @Override
    public Dynamics getDynamics(BigInteger did) {
        Dynamics dynamics = new Dynamics();
        // 获取动态主体
        dynamics.setDynamic(dynamicMapper.getData(did));
        // 如果没有获取到数据
        if (dynamics.getDynamic() == null) {
            return null;
        }
        // 获取动态附加信息
        dynamics.setDinfos(dynamicMapper.getInfo(did));

        // 获取操作对象昵称和头像
        for (Dinfo i : dynamics.getDinfos())
        {
            i.setName(userMapper.getName(i.getId()));
            i.setImage(userMapper.getImage(i.getId()));
        }

        // 如果动态的类型是转发类型
        if (dynamics.getDynamic().getType() == 1) {
            // 获取原始动态的did
            BigInteger originalDid = new BigInteger(dynamics.getDynamic().getData());
            // 获取原始动态的data
            dynamics.getDynamic().setData(dynamicMapper.getData(originalDid).getData());
            // 获取原始动态的media
            dynamics.getDynamic().setMedia(dynamicMapper.getData(originalDid).getMedia());
        }

        // 获取点赞、评论转发的数量
        dynamics.setLikeNums(dynamicMapper.getLikeNums(dynamics.getDynamic().getDid()));
        dynamics.setCommentNums(dynamicMapper.getCommentNums(dynamics.getDynamic().getDid()));
        dynamics.setForwardNums(dynamicMapper.getForwardNums(dynamics.getDynamic().getDid()));

        dynamics.setImage(dynamicMapper.getUserImage(dynamicMapper.getUserId(dynamics.getDynamic().getDid())));
        dynamics.setName(userMapper.getName(dynamicMapper.getUserId(dynamics.getDynamic().getDid())));

        // 获取动态标签
        dynamics.setLabel(dynamicMapper.getLabel(dynamics.getDynamic().getDid()));

        // 返回完整的动态信息
        return dynamics;
    }

    @Override
    public Dynamics getDynamics(String label) {
        BigInteger did = dynamicMapper.getDidByLabel(label);
        return getDynamics(did);
    }

    @Override
    public void sendDynamic(Dynamic dynamic) {
        // 发送动态
        dynamicMapper.sendDynamic(dynamic.getId(), dynamic.getType(), dynamic.getData(), dynamic.getMedia());
    }

    @Override
    public void deleteDynamic(BigInteger did) {
        // 删除did对应的动态
        dynamicMapper.deleteDynamic(did);
        // 消除转发
        dynamicMapper.deleteForward(did);
    }

    @Override
    public void doLike(BigInteger did, String id) {
        Dinfo dinfo = new Dinfo();
        // 动态id，did
        dinfo.setDid(did);
        // 点赞，附加信息类型：0=点赞，1=评论，2=转发
        dinfo.setType(0);
        // 点赞用户id
        dinfo.setId(id);
        // 时间和内容置空，时间会由mysql生成
        dinfo.setTime(null);
        dinfo.setData("null");
        dynamicMapper.doDinof(dinfo.getDid(),
                dinfo.getType(),
                dinfo.getId(),
                dinfo.getData());
    }

    @Override
    public void cancelLike(BigInteger did, String id) {
        dynamicMapper.cancelLike(did, id);
    }

    @Override
    public void doComment(BigInteger did, String id, String data) {
        Dinfo dinfo = new Dinfo();
        dinfo.setDid(did);
        // 评论
        dinfo.setType(1);
        dinfo.setId(id);
        dinfo.setTime(null);
        // 添加评论内容
        dinfo.setData(data);
        dynamicMapper.doDinof(dinfo.getDid(),
                dinfo.getType(),
                dinfo.getId(),
                dinfo.getData());
    }

    @Override
    public void deleteComment(BigInteger did, String id, String data) {
        dynamicMapper.deleteComment(did, id, data);
    }

    @Override
    public void doForward(BigInteger did, String id) {
        Dinfo dinfo = new Dinfo();
        dinfo.setDid(did);
        // 转发
        dinfo.setType(2);
        dinfo.setId(id);
        dinfo.setTime(null);
        dinfo.setData("null");
        dynamicMapper.doDinof(dinfo.getDid(),
                dinfo.getType(),
                dinfo.getId(),
                dinfo.getData());

        // 创建要转发的动态
        Dynamic dynamic = new Dynamic();
        // 用户id
        dynamic.setId(id);
        // 动态id，会由mysql生成
        dynamic.setDid(null);
        // 转发类型，动态类型：0=原创，1=转发
        dynamic.setType(1);
        // 时间置空，会由mysql生成
        dynamic.setTime(null);
        // 将消息文本内容设置为所转发动态的did
        dynamic.setData(did.toString());
        // 置空动态媒体信息
        dynamic.setMedia(null);
        // 发送动态（转发类型）
//         dynamicMapper.sendDynamic(dynamic);
        dynamicMapper.sendDynamic(dynamic.getId(), dynamic.getType(), dynamic.getData(), dynamic.getMedia());
    }

    @Override
    public List<BigInteger> searchDynamic(String keyword) {
        keyword = "%" + keyword + "%";
        List<BigInteger> res = dynamicMapper.searchDynamic(keyword);
        return res;
    }
}
